package likou.tanxin;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/10/20 08:07
 */
public class _738 {
    public static void main(String[] args) {
        System.out.println(monotoneIncreasingDigits(1234));
    }
    public static int monotoneIncreasingDigits(int N) {
        Deque<Integer> deque = new ArrayDeque<>();
        int daan = 0;
        int p = 1;
        while(N!=0){
            deque.offerFirst(N%10);
            N/=10;
        }
        if(deque.size()==0){
            return 0;
        }
        //从后往前遍历
        int lastNum = deque.pollLast() ;
        if(deque.size()>0){
            while(deque.size()>0){
                int now = deque.pollLast();
                if(lastNum>=now){
                    daan+=(lastNum*p);
                    p*=10;
                    lastNum = now;
                }else{
                    daan+=(9*p);
                    p*=10;
                    lastNum = now-1;
                }

            }
        }else{
            return lastNum;
        }
        daan+=(lastNum*p);
        return daan;
    }
}
